/*
 * @(#)InvokeUtil.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.util;

import java.util.Map;

import com.nhncorp.usf.core.call.ReturnValue;
import com.nhncorp.usf.core.invoker.ConnectorHolder;
import com.nhncorp.usf.core.invoker.InvocationConnectorManager;
import com.nhncorp.usf.core.invoker.InvocationFuture;
import com.nhncorp.usf.core.service.ExecutionException;

/**
 * @author Web Platform Development Team
 */
public class InvokeUtil {
    /**
     * run.xml에 정의한 bo를 수행
     * 
     * @param connectorId configuration.xml에 설정한 connector의 Id
     * @param id  BO object name
     * @param methodName  BO의 method name
     * @param methodParameter BO에 전달할 파라미터
     * @return BO의 리턴값
     * @throws Exception BO에서 발생한 Exception
     */
    public static Object serviceCall(String connectorId, String id, String methodName, Map<String, Object> methodParameter) throws Exception {
        ConnectorHolder invoker = InvocationConnectorManager.getInstance().getRBConnectorHolder(connectorId);
        Object result;

        try {
            InvocationFuture future = invoker.invoke(id, methodName, methodParameter);
            result = future.await().getReturnValue();
        } catch (Exception e) {
            ExecutionException exception = new ExecutionException(e);
            exception.setTargetName(id + "/" + methodName);
            throw exception;
        }

        if (result instanceof ReturnValue) {
            return ((ReturnValue) result).get();
        }

        return null;
    }
}
